<?php
namespace app\yunda\home;

use app\yunda\model\Qiandao as QiandaoModel;
use app\yunda\model\User;
use app\yunda\model\ScoreRecord;
use app\yunda\model\System;
use think\Db;

/**
 * 用户签到
 * @package app\yunda\home
 */
class Qiandao extends Base {
    // 获取列表
    public function index($openid=''){
        if(empty($openid))return mogo_error('参数错误',404);
        // 查询字段
        $map = $this->getMap();
        $map['get_score']=['notin',0];
        $map['openid']=$openid;
        $Address = new QiandaoModel();
        // 自定义where查询
        $data =  $Address
            ->order('id desc')
            ->where($map)
            ->paginate()
            ->each(function($item){
                $item['create_time']=format_time($item['create_time'],'Y.m.d');
                //$item['day_num']='连续签到第'.$item['day_num'].'天';
                return $item;
            });
        $data = $data->toArray();

        $row=User::get(['openid'=>$openid]);
        $row_1=QiandaoModel::where('openid',$openid)->order('create_time')->select();
        $row_2=Db::table('dp_yd_qiandao')->where('openid',$openid)->whereTime('create_time','d')->find();//今日是否签到
        if(empty($row_1)) $row_1[0]['day_num']=0;
        if($row_2){
            $is_sign=1;
        }else{
            $is_sign=0;
        }
        $extra=[
            'score'=>$row['score'],
            'day'=>$row_1[0]['day_num'],
            'is_sign'=>$is_sign,
        ];
        return mogo_json($data,'',$extra);
    }
    // 签到
//    public function save(){
//        // 第一步 过滤字段
//        $data = request()->only(['openid']);
//        // 第二部 验证字段
//        $result = $this->validate($data,'Qiandao');
//        if(true!==$result){
//            return mogo_error($result);
//        }
//        $row=Db::table('dp_yd_qiandao')
//            ->whereTime('create_time','d')
//            ->where(['openid'=>$data['openid']])
//            ->find();
//        if(!empty($row)){
//            return mogo_error('您今天已经签到',404);
//        }
//        $user=User::get(['openid'=>$data['openid']]);
//        //获取用户是否初次签到
//        $list=QiandaoModel::where(['openid'=>$data['openid']])->order('create_time desc')->select();
//
//        //上次签到时间
//        if(empty($list)){
//            $data['last_qd_time']=time();
//        }else{
//            $data['last_qd_time']=$list[0]['create_time'];
//        }
//        //获取设定连续签到n天获得m积分
//        $config=[
//            'sign_score'=>System::getSystemConfig('sign_score')['config_value'],
//            'sign_day'=>System::getSystemConfig('sign_day')['config_value'],
//            'sign_score_default'=>System::getSystemConfig('sign_score_default')['config_value'],
//            'sign_day_default'=>System::getSystemConfig('sign_day_default')['config_value'],
//        ];
//       // halt($config);
//        //获取用户到今天的连续签到天数
//        $num=1;
//        foreach ($list as $k=>$v){
//            $c_day=$v['create_time']-$v['last_qd_time'];
//            $day=86400;//一天的秒数
//            $s=$c_day/86400;
//            if($s<=1){
//                $num+=1;
//            } else{
//                break;
//            }
//        }
//        if($config['sign_day']>$config['sign_day_default']){
//            $yushu=$num%$config['sign_day'];
//            $data['get_score']=$config['sign_score'];
//        }else{
//            $yushu=$num%$config['sign_day_default'];
//            $data['get_score']=$config['sign_score_default'];
//        }
//
//        $data['day_num']=$yushu;
//       // halt($data);
//        $res = QiandaoModel::create($data);
//        //更新用户积分
//        $user['score']+=$res['get_score'];
//        $user->update(['score'=>$user['score'],'id'=>$user['id']]);
//        $recharge='签到获得积分'.$res['get_score'];
//        ScoreRecord::createScoreRecord($recharge,$res['get_score'],$user['id']);
//        if($res['get_score']==0)return mogo_json($res['day_num'],'',1);
//        return mogo_json($res,'',1);
//    }

    //签到
    public function save()
    {
        // 第一步 过滤字段
        $data = request()->only(['openid']);
        // 第二部 验证字段
        $result = $this->validate($data,'Qiandao');
        if(true!==$result){
            return mogo_error($result);
        }
        $row=Db::table('dp_yd_qiandao')
            ->whereTime('create_time','d')
            ->where(['openid'=>$data['openid']])
            ->find();
        if(!empty($row)){
            return mogo_error('您今天已经签到',404);
        }
        $user=User::get(['openid'=>$data['openid']]);
        //获取用户是否初次签到
        $list=QiandaoModel::where(['openid'=>$data['openid']])->order('create_time desc')->select();

        //上次签到时间
        if(empty($list)){
            $data['last_qd_time']=time();
        }else{
            $data['last_qd_time']=$list[0]['create_time'];
        }
        //获取设定连续签到n天获得m积分
        $sign_config=collection(System::where(['config_type'=>'sign'])
            ->order('config_name')->field('config_name,config_value')->select())->toArray();


        //获取用户到今天的连续签到天数
        $num=0;
        if(!empty($list)){
            foreach ($list as $k=>$v){
                $c_day=$v['create_time']-$v['last_qd_time'];
                $day=86400;//一天的秒数
                $s=$c_day/86400;
                if($s<=1){//签到时间间隔少于一天
                    $num=$v['day_num']+1;
                } else{//签到时间间隔大于一天,重新计算
                    $num=1;
                }
            }
        }else{
            $num=1;
        }

        // halt($sign_config);

        foreach ($sign_config as $v1){
            if($num==$v1['config_name']){
                $data['get_score']=$v1['config_value'];
            }else{
                $data['get_score']=$sign_config[0]['config_value'];
            }
        }
        $data['day_num']=$num;
        //halt($data);
        $res = QiandaoModel::create($data);
        //更新用户积分
        $user['score']+=$res['get_score'];
        $user->update(['score'=>$user['score'],'id'=>$user['id']]);
        $recharge='签到获得积分'.$res['get_score'];
        ScoreRecord::createScoreRecord($recharge,$res['get_score'],$user['id']);
        if($res['get_score']==0)return mogo_json($res['day_num'],'',1);
        return mogo_json($res,'',1);
    }








}
